package com.ruoyi.web.controller.deal;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.deal.bo.HyDealGoodsAddBo;
import com.ruoyi.deal.bo.HyDealGoodsEditBo;
import com.ruoyi.deal.bo.HyDealGoodsQueryBo;
import com.ruoyi.deal.service.IHyDealGoodsService;
import com.ruoyi.deal.vo.HyDealGoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 商品管理Controller
 *
 * @author lixin
 * @date 2021-05-16
 */
@Api(value = "商品管理控制器" , tags = {"商品管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/deal/goods")
public class HyDealGoodsController extends BaseController {

	private final IHyDealGoodsService dealGoodsService;

	/**
	 * 查询商品管理列表
	 */
	@ApiOperation("查询商品管理列表")
	@PreAuthorize("@ss.hasPermi('deal:goods:list')")
	@GetMapping("/list")
	public TableDataInfo<HyDealGoodsVo> list(HyDealGoodsQueryBo bo) {
		return dealGoodsService.queryPageList(bo);
	}

	/**
	 * 导出商品管理列表
	 */
	@ApiOperation("导出商品管理列表")
	@PreAuthorize("@ss.hasPermi('deal:goods:export')")
	@Log(title = "商品管理" , businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<HyDealGoodsVo> export(HyDealGoodsQueryBo bo) {
		List<HyDealGoodsVo> list = dealGoodsService.queryList(bo);
		ExcelUtil<HyDealGoodsVo> util = new ExcelUtil<HyDealGoodsVo>(HyDealGoodsVo.class);
		return util.exportExcel(list, "商品管理");
	}

	/**
	 * 获取商品管理详细信息
	 */
	@ApiOperation("获取商品管理详细信息")
	@PreAuthorize("@ss.hasPermi('deal:goods:query')")
	@GetMapping("/{goodsId}")
	public AjaxResult<HyDealGoodsVo> getInfo(@PathVariable("goodsId") Integer goodsId) {
		return AjaxResult.success(dealGoodsService.queryById(goodsId));
	}

	/**
	 * 新增商品管理
	 */
	@ApiOperation("新增商品管理")
	@PreAuthorize("@ss.hasPermi('deal:goods:add')")
	@Log(title = "商品管理" , businessType = BusinessType.INSERT)
	@PostMapping()
	public AjaxResult<Void> add(@RequestBody HyDealGoodsAddBo bo) {
		return toAjax(dealGoodsService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改商品管理
	 */
	@ApiOperation("修改商品管理")
	@PreAuthorize("@ss.hasPermi('deal:goods:edit')")
	@Log(title = "商品管理" , businessType = BusinessType.UPDATE)
	@PutMapping()
	public AjaxResult<Void> edit(@RequestBody HyDealGoodsEditBo bo) {
		return toAjax(dealGoodsService.updateByEditBo(bo) ? 1 : 0);
	}

	/**
	 * 删除商品管理
	 */
	@ApiOperation("删除商品管理")
	@PreAuthorize("@ss.hasPermi('deal:goods:remove')")
	@Log(title = "商品管理" , businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
	public AjaxResult<Void> remove(@PathVariable Integer[] goodsIds) {
		return toAjax(dealGoodsService.deleteWithValidByIds(Arrays.asList(goodsIds), true) ? 1 : 0);
	}
}
